home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / src / symtab.h < prev    next >
C/C++ Source or Header  |  1996-10-13  |  8KB  |  331 lines

  1. /*
  2.  
  3. Copyright (C) 1996 John W. Eaton
  4.  
  5. This file is part of Octave.
  6.  
  7. Octave is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. Octave is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Octave; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. */
  22.  
  23. #if !defined (octave_symtab_h)
  24. #define octave_symtab_h 1
  25.  
  26. #if defined (__GNUG__)
  27. #pragma interface
  28. #endif
  29.  
  30. #include <string>
  31.  
  32. #include "SLStack.h"
  33.  
  34. #include "str-vec.h"
  35.  
  36. #include "variables.h"
  37.  
  38. // Must be multiple of 2.
  39. #define HASH_TABLE_SIZE 1024
  40. #define HASH_MASK (HASH_TABLE_SIZE - 1)
  41.  
  42. class tree;
  43. class tree_fvc;
  44. class tree_builtin;
  45. class tree_constant;
  46. class tree_function;
  47.  
  48. class string_vector;
  49.  
  50. class symbol_def;
  51. class symbol_record;
  52. class symbol_record_info;
  53. class symbol_table;
  54.  
  55. // Variables or functions.
  56.  
  57. class symbol_def
  58. {
  59.   friend class symbol_record;
  60.   friend class symbol_record_info;
  61.  
  62. public:
  63.  
  64.   symbol_def (void);
  65.   symbol_def (tree_constant *t);
  66.   symbol_def (tree_builtin *t, unsigned fcn_type = 0);
  67.   symbol_def (tree_function *t, unsigned fcn_type = 0);
  68.  
  69.   ~symbol_def (void);
  70.  
  71.   int is_variable (void) const;
  72.   int is_function (void) const;
  73.   int is_text_function (void) const;
  74.   int is_mapper_function (void) const;
  75.   int is_user_variable (void) const;
  76.   int is_user_function (void) const;
  77.   int is_builtin_variable (void) const;
  78.   int is_builtin_function (void) const;
  79.   int is_map_element (const string& elts) const;
  80.  
  81.   void define (tree_constant *t);
  82.   void define (tree_builtin *t, unsigned fcn_type = 0);
  83.   void define (tree_function *t, unsigned fcn_type = 0);
  84.  
  85.   void protect (void);
  86.   void unprotect (void);
  87.   void make_eternal (void);
  88.  
  89.   tree_fvc *def (void) const;
  90.   string help (void) const;
  91.   void document (const string& h);
  92.  
  93.   enum TYPE
  94.     {
  95.       UNKNOWN = 0,
  96.       USER_FUNCTION = 1,
  97.       USER_VARIABLE = 2,
  98.       BUILTIN_FUNCTION = 4,
  99.       TEXT_FUNCTION = 8,
  100.       MAPPER_FUNCTION = 16,
  101.       BUILTIN_VARIABLE = 32
  102.     };
  103.  
  104.   friend maybe_delete (symbol_def *def);
  105.  
  106. private:
  107.  
  108.   unsigned type : 6;
  109.   unsigned eternal : 1;
  110.   unsigned read_only : 1;
  111.  
  112.   string help_string;
  113.   tree_fvc *definition;
  114.   symbol_def *next_elem;
  115.   int count;
  116.  
  117.   void init_state (void);
  118.  
  119.   symbol_def (const symbol_def& sd);
  120.   symbol_def& operator = (const symbol_def& sd);
  121. };
  122.  
  123. // Individual records in a symbol table.
  124.  
  125. class
  126. symbol_record
  127. {
  128.   friend class symbol_record_info;
  129.  
  130. public:
  131.   symbol_record (void);
  132.   symbol_record (const string& n, symbol_record *nxt = 0);
  133.  
  134.   ~symbol_record (void) { }
  135.  
  136.   string name (void) const;
  137.   string help (void) const; 
  138.   tree_fvc *def (void) const;
  139.  
  140.   void rename (const string& new_name);
  141.  
  142.   int is_function (void) const;
  143.   int is_user_function (void) const;
  144.   int is_text_function (void) const;
  145.   int is_mapper_function (void) const;
  146.   int is_builtin_function (void) const;
  147.   int is_variable (void) const;
  148.   int is_user_variable (void) const;
  149.   int is_builtin_variable (void) const;
  150.   int is_map_element (const string& elts) const;
  151.  
  152.   unsigned type (void) const;
  153.  
  154.   int is_defined (void) const;
  155.   int is_read_only (void) const;
  156.   int is_eternal (void) const;
  157.  
  158.   void protect (void);
  159.   void unprotect (void);
  160.   void make_eternal (void);
  161.  
  162.   void set_sv_function (sv_Function f);
  163.  
  164.   int define (tree_constant *t);
  165.   int define (const octave_value& v);
  166.   int define (tree_builtin *t, int text_fcn = 0);
  167.   int define (tree_function *t, int text_fcn = 0);
  168.   int define_as_fcn (const octave_value& v);
  169.   int define_builtin_var (const octave_value& v);
  170.  
  171.   void document (const string& h);
  172.  
  173.   int clear (void);
  174.  
  175.   void alias (symbol_record *s, int force = 0);
  176.  
  177.   void mark_as_formal_parameter (void);
  178.   int is_formal_parameter (void) const;
  179.  
  180.   void mark_as_linked_to_global (void);
  181.   int is_linked_to_global (void) const;
  182.  
  183.   octave_value variable_value (void) const;
  184.   octave_value& variable_reference (void);
  185.  
  186.   symbol_record *next (void) const;
  187.  
  188.   void chain (symbol_record *s);
  189.  
  190.   void push_context (void);
  191.   void pop_context (void);
  192.  
  193. private:
  194.  
  195.   unsigned formal_param : 1;
  196.   unsigned linked_to_global : 1;
  197.  
  198.   string nm;
  199.   sv_Function sv_fcn;
  200.   symbol_def *definition;
  201.   symbol_record *next_elem;
  202.  
  203. // This should maybe be one stack with a structure containing all the
  204. // items we need to save for recursive calls...
  205.   SLStack <symbol_def *> context;
  206.   SLStack <unsigned> global_link_context;
  207.  
  208.   void init_state (void);
  209.  
  210.   int read_only_error (void);
  211.  
  212.   void push_def (symbol_def *sd);
  213.   symbol_def *pop_def (void);
  214.  
  215.   symbol_record& operator = (const symbol_record& s);
  216. };
  217.  
  218. // A structure for handling verbose information about a symbol_record.
  219.  
  220. class
  221. symbol_record_info
  222. {
  223. public:
  224.  
  225.   symbol_record_info (void);
  226.   symbol_record_info (const symbol_record& s);
  227.  
  228.   symbol_record_info (const symbol_record_info& s);
  229.  
  230.   ~symbol_record_info (void) { }
  231.  
  232.   symbol_record_info& operator = (const symbol_record_info& s);
  233.  
  234.   int is_defined (void) const;
  235.   int is_read_only (void) const;
  236.   int is_eternal (void) const;
  237.   int hides_fcn (void) const;
  238.   int hides_builtin (void) const;
  239.   string type_name (void) const;
  240.   int is_function (void) const;
  241.   int rows (void) const;
  242.   int columns (void) const;
  243.   string name (void) const;
  244.  
  245.   enum HIDES
  246.     {
  247.       SR_INFO_NONE = 0,
  248.       SR_INFO_USER_FUNCTION = 1,
  249.       SR_INFO_BUILTIN_FUNCTION = 2
  250.     };
  251.  
  252. private:
  253.  
  254.   int initialized;
  255.   int nr;
  256.   int nc;
  257.   unsigned type : 6;
  258.   unsigned hides : 2;
  259.   unsigned eternal : 1;
  260.   unsigned read_only : 1;
  261.   string nm;
  262.   string const_type;
  263. };
  264.  
  265. // A symbol table.
  266.  
  267. #define SYMTAB_LOCAL_SCOPE 1
  268. #define SYMTAB_GLOBAL_SCOPE 2
  269.  
  270. #define SYMTAB_ALL_SCOPES (SYMTAB_LOCAL_SCOPE | SYMTAB_GLOBAL_SCOPE)
  271.  
  272. #define SYMTAB_ALL_TYPES (symbol_def::USER_FUNCTION \
  273.               | symbol_def::USER_VARIABLE \
  274.               | symbol_def::BUILTIN_FUNCTION \
  275.               | symbol_def::TEXT_FUNCTION \
  276.               | symbol_def::MAPPER_FUNCTION \
  277.               | symbol_def::BUILTIN_VARIABLE)
  278.  
  279. #define SYMTAB_VARIABLES (symbol_def::USER_VARIABLE \
  280.               | symbol_def::BUILTIN_VARIABLE)
  281.  
  282. class
  283. symbol_table
  284. {
  285. public:
  286.  
  287.   symbol_table (void);
  288.  
  289.   symbol_record *lookup (const string& nm, int insert = 0, int warn = 0);
  290.  
  291.   void rename (const string& old_name, const string& new_name);
  292.  
  293.   void clear (int clear_user_functions = 1);
  294.   int clear (const string& nm, int clear_user_functions = 1);
  295.  
  296.   int size (void) const;
  297.  
  298.   symbol_record_info *
  299.   long_list (int& count, const string_vector& pats = string_vector (),
  300.          int npats = 0, int sort = 0, unsigned type = SYMTAB_ALL_TYPES,
  301.          unsigned scope = SYMTAB_ALL_SCOPES) const;
  302.  
  303.   string_vector
  304.   list (int& count, const string_vector& pats = string_vector (),
  305.     int npats = 0, int sort = 0, unsigned type = SYMTAB_ALL_TYPES,
  306.     unsigned scope = SYMTAB_ALL_SCOPES) const;
  307.  
  308.   symbol_record **glob (int& count, const string& pat = string ("*"),
  309.             unsigned type = SYMTAB_ALL_TYPES,
  310.             unsigned scope = SYMTAB_ALL_SCOPES) const;
  311.  
  312.   void push_context (void);
  313.   void pop_context (void);
  314.  
  315. private:
  316.  
  317.   unsigned int hash (const string& s);
  318.  
  319.   symbol_record table[HASH_TABLE_SIZE];
  320. };
  321.  
  322. extern int valid_identifier (const char *s);
  323.  
  324. #endif
  325.  
  326. /*
  327. ;;; Local Variables: ***
  328. ;;; mode: C++ ***
  329. ;;; End: ***
  330. */
  331.